﻿using Communication.Channel;
using System;
using System.Threading;

namespace Communication.Bootstrap
{
	/// <summary>
	/// 客户端连接引导程序
	/// </summary>
	public class ClientBootstrap : Bootstrap
	{
		// Token: 0x0600003B RID: 59 RVA: 0x00002170 File Offset: 0x00000370
		public ClientBootstrap()
		{
          
        }

        // Token: 0x0600003C RID: 60 RVA: 0x00002178 File Offset: 0x00000378
        public ClientBootstrap(ChannelFactory channelFactory) : base(channelFactory)
		{
		}
		/// <summary>
		/// 开启串口通讯
		/// </summary>
		/// <param name="remoteAddress"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException"></exception>
		public Communication.Channel.Channel connect(Address remoteAddress)
		{
			if (remoteAddress == null)
			{
				throw new ArgumentNullException("remotedAddress");
			}
            Communication.Channel.Channel result;
			try
			{
                Communication.Channel.Channel channel = base.GetFactory().NewChannel();
				channel.GetConfig().SetOptions(base.GetOptions());
		
                channel.Connect(remoteAddress);
				while (!channel.IsConnected())
				{
					Thread.Sleep(1000);
				}
				result = channel;
			}
			catch
			{
				throw;
			}
			return result;
		}


        /// <summary>
        /// 开启串口通讯
        /// </summary>
        /// <param name="remoteAddress"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public void Disconnect()
        {
         
            try
            {
                Communication.Channel.Channel channel = base.GetFactory().NewChannel();

                channel.Disconnect();
            
            }
            catch
            {
                throw;
            }
        }
    }
}
